<?php

namespace App\Http\Middleware;

use App\Models\Adsense;
use Closure;

class HomeAdsense
{
    public function handle($request, Closure $next)
    {
        view()->share('ads',$this->getAds());
        return $next($request);
    }


    protected function getAds(){
        $ads = Adsense::select(['id','title','thumb','link','target','group'])->where('status',Adsense::STATUS_1)
            ->orderBy('id', 'asc')->get()->toArray();
        $_ads = [];
        foreach ($ads as $ad){
            $_ads[$ad['group']][] = $ad;
        }
        return $_ads;
    }
}
